
VUEADF.policy.XYCompositeLayoutPolicy = function(){
    VUEADF.policy.XYCompositeLayoutPolicy.superclass.constructor.call(this);
};

COM.util.defineClass(VUEADF.policy.XYCompositeLayoutPolicy, COM.gef.XYLayoutEditPolicy, {
    createChangeConstraintCommand: function(request, child, rect){
      var model = child.getModel(),
          parentFigure = child.parent.getFigure().getBounds();
      if(rect.width < 30 || rect.height < 30) {
        return COM.gef.Command.UNEXECUTABLE_COMMAND;
      }

      return COM.gef.Command.createCommand({
        label: 'change bounds',
        execute: function(){
          this.old = model.getLayoutData();
          var boundWidth = parentFigure.width - rect.width,
              boundHeight = parentFigure.height - rect.height;
          if((rect.x > 0 &&rect.x < boundWidth) && (rect.y > 0 &&rect.y < boundHeight)){
              model.setLayoutData(rect);
          }else{
              model.setLayoutData(this.old);
          }
        },
        undo: function(){
          model.setLayoutData(this.old);
        }
      });

    },
    getCreateCommand: function(request){
      return COM.gef.Command.createCommand({
        label: 'create child',
        child: request.getNewObject(),
        parent: this.getHost().getModel(),
        figure: this.getHost().getFigure(),
        execute: function(){
          var models;
          if(this.child instanceof VUEADF.model.CompoundModel){
            models = this.child.getModels();
          }else{
            models = [this.child];
          }
          var copy = this.parent.getLayoutData().copy();
          this.figure.translateToAbsolute(copy);
          var relativeLoaction = new COM.draw2d.Point(request.location.x - copy.x, request.location.y - copy.y);
          var rel;
          for (var i = 0; i < models.length; i++) {
            var model = models[i];
            if(i > 0){
              relativeLoaction.x = relativeLoaction.x + models[i - 1].getLayoutData().width - 1;
            }
            this.parent.addChild(model);
            var bounds = new COM.draw2d.Rectangle();
            bounds.width = 63;
            bounds.height = 33;
            bounds.setLocation(relativeLoaction);
            model.setLayoutData(bounds);
          }
        },
        undo: function(){
          var models;
          if(this.child instanceof VUEADF.model.CompoundModel){
            models = this.child.getModels();
          }else{
            models = [this.child];
          }
          for (var i = 0; i < models.length; i++) {
            var model = models[i];
            this.parent.removeChild(model);
          }
        }
      });
    },
    createAddCommand: function(request, child, rect) {
      var command = new COM.gef.CompoundCommand(), deleteCommand = null, addCommand = null;
      deleteCommand = COM.gef.Command.createCommand({
        label: 'delete model',
        parentModel: child.getParent().getModel(),
        childModel:child.getModel(),
        oldBounds: child.getModel().getLayoutData(),
        execute: function(){
          this.parentModel.removeChild(this.childModel);
        },
        undo: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(this.oldBounds);
        }
      });
      addCommand = COM.gef.Command.createCommand({
        label: 'add model',
        parentModel: this.getHost().getModel(),
        childModel:child.getModel(),
        execute: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(rect);
        },
        undo: function(){
          this.parentModel.removeChild(this.childModel);
        }
      });
      command.add(deleteCommand);
      command.add(addCommand);
      return command.unwrap();
    },
    getConstraintFor: function(request, child) {
      var rectangle = new COM.draw2d.Rectangle(), childBoundsCopy = child.getModel().getLayoutData().copy(),
          hostModel = this.getHost().getModel(), copy = hostModel.getLayoutData().copy(),
          hostFigure = this.getHost().getFigure(), childFigure = child.getFigure(), moveData = request.getMoveDelta();
      hostFigure.translateToAbsolute(copy);
      childFigure.translateToAbsolute(childBoundsCopy);
      var relativeLoaction = new COM.draw2d.Point(childBoundsCopy.x + moveData.x - copy.x  , childBoundsCopy.y + moveData.y - copy.y);
      rectangle.setLocation(relativeLoaction);
      rectangle.setSize(child.getFigure().getLayoutData().getSize());
      return rectangle;
    }
});